
import java.awt.event.*;
import java.awt.*;
import java.io.*;

class Entrernom extends Dialog{
  Button valid;
  TextField nom;
  Label l,l2;
  int score;

 Entrernom(Frame f,String s,int score){
    super(f,s);
    this.score=score;
    setLayout(new BorderLayout ());
     setBackground(Color.magenta);
     setBounds(100,100,250,150);
     setResizable(false);
     Panel pp=new Panel(new GridLayout (3,1)) ;
     Panel pl=new Panel(new FlowLayout(FlowLayout.CENTER )) ;
     Panel plt=new Panel(new FlowLayout(FlowLayout.RIGHT )) ;
     Panel pb=new Panel(new FlowLayout(FlowLayout.CENTER )) ;
     plt.setFont(new Font("Solution",Font.BOLD ,11)) ;
     pl.setFont(new Font("Solution",Font.BOLD ,15)) ;
     l2=new Label("MEILLEUR SCORE !!") ;
     valid=new Button("VALIDER") ;
     l=new Label("Entrer votre nom :");
     nom=new TextField(10);
     nom.setBackground(Color.white) ;
     nom.requestFocus() ;
     pl.add(l2);
     plt.add(l);
     plt.add(nom);
     pb.add(valid);
     pp.add(pl);
     pp.add(plt);
     pp.add(pb);
     add(pp,"Center");
     addWindowListener(new FermerFenetre());
     valid.addActionListener(new TraitNomScores(nom,score)) ;
     valid.addActionListener(new Tfermer());
     setVisible(true);
   }

  class Tfermer implements ActionListener{
    public void actionPerformed(ActionEvent e){
     dispose();
    }
}
}
class TraitNomScores implements ActionListener{
  TextField nom;int score;
  TraitNomScores(TextField nom,int score){
    this.nom=nom;
    this.score=score;
  }
    public void actionPerformed(ActionEvent e){
      try{
        DataOutputStream ftomp = new DataOutputStream(new FileOutputStream("tompo"));
        DataInputStream fout = new DataInputStream(new FileInputStream("nomutilisateur"));
        try{

          while (true)
          {
            ftomp.writeUTF(fout.readUTF());
            ftomp.writeInt(fout.readInt());
          }
        }catch(EOFException d)
          { ftomp.close() ;
             fout.close() ;
           }

      }catch(IOException ee){}
  try{
     DataOutputStream fout = new DataOutputStream(new FileOutputStream("nomutilisateur"));
     fout.writeUTF(nom.getText()) ;
     fout.writeInt(score) ;
     fout.close() ;
   }catch(IOException ee){}
   try{
        DataInputStream ftomp = new DataInputStream(new FileInputStream("tompo"));
        DataInputStream fout = new DataInputStream(new FileInputStream("nomutilisateur"));
        DataOutputStream ftompP = new DataOutputStream(new FileOutputStream("tompoP"));

        ftompP.writeUTF(fout.readUTF());
        ftompP.writeInt(fout.readInt());

        try{
          for(int i=5;i>0;i--)//pour ne pas depasser 7 meilleurs scores
          {
         ftompP.writeUTF(ftomp.readUTF());
         ftompP.writeInt(ftomp.readInt());
       }
     }catch(EOFException f){}

        ftomp.close() ;
        fout.close() ;
        ftompP.close();

 }catch(IOException ee){}

   try{
        DataOutputStream fout = new DataOutputStream(new FileOutputStream("nomutilisateur"));
        DataInputStream ftompP = new DataInputStream(new FileInputStream("tompoP"));
        try{
          while(true){
            fout.writeUTF(ftompP.readUTF()) ;
            fout.writeInt(ftompP.readInt()) ;
          }
        }catch(EOFException r){
          fout.close();
          ftompP.close();
        }
   }catch(IOException ee){}
 }}




